home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / content / emailcloak.php next >
PHP Script  |  2008-07-06  |  6KB  |  167 lines

  1. <?php
  2. /**
  3. * @version        $Id: emailcloak.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package        oomla
  5. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license        GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. $mainframe->registerEvent('onPrepareContent', 'plgContentEmailCloak');
  18.  
  19. /**
  20.  * Plugin that cloaks all emails in content from spambots via Javascript.
  21.  *
  22.  * @param object|string An object with a "text" property or the string to be
  23.  * cloaked.
  24.  * @param array Additional parameters. See {@see plgEmailCloak()}.
  25.  * @param int Optional page number. Unused. Defaults to zero.
  26.  * @return boolean True on success.
  27.  */
  28. function plgContentEmailCloak(&$row, &$params, $page=0)
  29. {
  30.     if (is_object($row)) {
  31.         return plgEmailCloak($row->text, $params);
  32.     }
  33.     return plgEmailCloak($row, $params);
  34. }
  35.  
  36. /**
  37.  * Genarate a search pattern based on link and text.
  38.  *
  39.  * @param string The target of an e-mail link.
  40.  * @param string The text enclosed by the link.
  41.  * @return string A regular expression that matches a link containing the
  42.  * parameters.
  43.  */
  44. function plgContentEmailCloak_searchPattern ($link, $text) {
  45.     // <a href="mailto:anyLink">anyText</a>
  46.     $pattern = '~(?:<a [\w "\'=\@\.\-]*href\s*=\s*"mailto:'
  47.         . $link . '"[\w "\'=\@\.\-]*)>' . $text . '</a>~i';
  48.  
  49.     return $pattern;
  50. }
  51.  
  52. /**
  53.  * Cloak all emails in text from spambots via Javascript.
  54.  *
  55.  * @param string The string to be cloaked.
  56.  * @param array Additional parameters. Parameter "mode" (integer, default 1)
  57.  * replaces addresses with "mailto:" links if nonzero.
  58.  * @return boolean True on success.
  59.  */
  60. function plgEmailCloak(&$text, &$params)
  61. {
  62.  
  63.     /*
  64.      * Check for presence of {emailcloak=off} which is explicits disables this
  65.      * bot for the item.
  66.      */
  67.     if (JString::strpos($text, '{emailcloak=off}') !== false) {
  68.         $text = JString::str_ireplace('{emailcloak=off}', '', $text);
  69.         return true;
  70.     }
  71.  
  72.     // Simple performance check to determine whether bot should process further.
  73.     if (JString::strpos($text, '@') === false) {
  74.         return true;
  75.     }
  76.  
  77.     $plugin = & JPluginHelper::getPlugin('content', 'emailcloak');
  78.  
  79.     // Load plugin params info
  80.     $pluginParams = new JParameter($plugin->params);
  81.     $mode = $pluginParams->def('mode', 1);
  82.  
  83.     // any@email.address.com
  84.     $searchEmail = '([\w\.\-]+\@(?:[a-z0-9\.\-]+\.)+(?:[a-z0-9\-]{2,4}))';
  85.     // any@email.address.com?subject=anyText
  86.     $searchEmailLink = $searchEmail . '([?&][\x20-\x7f][^"<>]+)';
  87.     // anyText
  88.     $searchText = '([\x20-\x7f][^<>]+)';
  89.  
  90.     /*
  91.      * Search for derivatives of link code <a href="mailto:email@amail.com"
  92.      * >email@amail.com</a>
  93.      */
  94.     $pattern = plgContentEmailCloak_searchPattern($searchEmail, $searchEmail);
  95.     while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  96.         $mail = $regs[1][0];
  97.         $mailText = $regs[2][0];
  98.  
  99.         // Check to see if mail text is different from mail addy
  100.         $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText);
  101.  
  102.         // Replace the found address with the js cloaked email
  103.         $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  104.     }
  105.  
  106.     /*
  107.      * Search for derivatives of link code <a href="mailto:email@amail.com">
  108.      * anytext</a>
  109.      */
  110.     $pattern = plgContentEmailCloak_searchPattern($searchEmail, $searchText);
  111.     while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  112.         $mail = $regs[1][0];
  113.         $mailText = $regs[2][0];
  114.  
  115.         $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText, 0);
  116.  
  117.         // Replace the found address with the js cloaked email
  118.         $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  119.     }
  120.  
  121.     /*
  122.      * Search for derivatives of link code <a href="mailto:email@amail.com?
  123.      * subject=Text">email@amail.com</a>
  124.      */
  125.     $pattern = plgContentEmailCloak_searchPattern($searchEmailLink, $searchEmail);
  126.     while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  127.         $mail = $regs[1][0] . $regs[2][0];
  128.         $mailText = $regs[3][0];
  129.         // Needed for handling of Body parameter
  130.         $mail = str_replace( '&', '&', $mail );
  131.  
  132.         // Check to see if mail text is different from mail addy
  133.         $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText);
  134.  
  135.         // Replace the found address with the js cloaked email
  136.         $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  137.     }
  138.  
  139.     /*
  140.      * Search for derivatives of link code <a href="mailto:email@amail.com?
  141.      * subject=Text">anytext</a>
  142.      */
  143.     $pattern = plgContentEmailCloak_searchPattern($searchEmailLink, $searchText);
  144.     while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  145.         $mail = $regs[1][0] . $regs[2][0];
  146.         $mailText = $regs[3][0];
  147.         // Needed for handling of Body parameter
  148.         $mail = str_replace('&', '&', $mail);
  149.  
  150.         $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText, 0);
  151.  
  152.         // Replace the found address with the js cloaked email
  153.         $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  154.     }
  155.  
  156.     // Search for plain text email@amail.com
  157.     $pattern = '~' . $searchEmail . '([^a-z0-9]|$)~i';
  158.     while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  159.         $mail = $regs[1][0];
  160.         $replacement = JHTML::_('email.cloak', $mail, $mode);
  161.  
  162.         // Replace the found address with the js cloaked email
  163.         $text = substr_replace($text, $replacement, $regs[1][1], strlen($mail));
  164.     }
  165.     return true;
  166. }
  167.